<?php
class EmailWrapper{
    // imap server connection
    private $conn;
    // inbox storage and inbox message count
    private $inbox;
    private $msg_cnt;

    // email login credentials
    private $server = 'imap.cash2u.ca';
    private $user   = 'careers@cash2u.ca';
    private $pass   = 'actW3740!@#$';
    private $port   = 110;
	private $protocol = '';
 
    // connect to the server and get the inbox emails
    function __construct($server, $user, $pass, $port, $proto) {
		$this->$server = $server;
		$this->user = $user;
		$this->pass = $pass;
		$this->port = $port;
		$this->protocol = $proto;
		
        $this->connect();
        $this->inbox();
    }
 
    // close the server connection
    function close() {
        $this->inbox = array();
        $this->msg_cnt = 0;
 
        imap_close($this->conn);
    }
 
    // open the server connection
    // the imap_open function parameters will need to be changed for the particular server
    // these are laid out to connect to a Dreamhost IMAP server
    function connect() {
        $this->conn = imap_open('{'.$this->server.':'.$this->port.$this->protocol.'}', $this->user, $this->pass);
    }
 
    // get a specific message (1 = first email, 2 = second email, etc.)
    function get($msg_index=NULL) {
        if (count($this->inbox) <= 0) {
            return array();
        }
        elseif (!is_null($msg_index) && isset($this->inbox[$msg_index])) {
            return $this->inbox[$msg_index];
        }

        return $this->inbox[0];
    }
 
    // read the inbox
    function inbox() {
        $this->msg_cnt = imap_num_msg($this->conn);

        $in = array();
        for($i = 1; $i <= $this->msg_cnt; $i++) {
            $in[] = array(
                'index'     => $i,
                'header'    => imap_headerinfo($this->conn, $i),
                'body'      => imap_body($this->conn, $i),
                'structure' => imap_fetchstructure($this->conn, $i)
            );
        }

        $this->inbox = $in;
    }
	
	function __toString(){
		return "to string called";
	}
}

$mailer = new EmailWrapper('imap.cash2u.ca','careers@cash2u.ca','actW3740!@#$','110','/pop3/novalidate-cert');
echo $mailer;
?>
